package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.SchoolGradeClassDao;
import com.zzgc.common.dao.SchoolGradeDao;
import com.zzgc.common.dao.StudentArchivesDao;
import com.zzgc.common.dao.StudentDao;
import com.zzgc.common.entity.SchoolGrade;
import com.zzgc.common.entity.SchoolGradeClass;
import com.zzgc.common.entity.Student;
import com.zzgc.common.entity.StudentArchives;
import com.zzgc.modules.controller.BaseController;
import com.zzgc.modules.service.SchoolGradeService;
import io.swagger.models.auth.In;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 学校年级(SchoolGrade)表服务实现类
 *
 * @author makejava
 * @since 2022-11-22 09:06:25
 */
@Service("schoolGradeService")
public class SchoolGradeServiceImpl implements SchoolGradeService {
    @Resource
    private SchoolGradeDao schoolGradeDao;
    @Resource
    private StudentArchivesDao archivesDao;
    @Resource
    private SchoolGradeClassDao schoolGradeClassDao;
    @Resource
    private StudentDao studentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SchoolGrade queryById(Long id) {
        return this.schoolGradeDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param schoolGrade 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SchoolGrade> queryByPage(SchoolGrade schoolGrade) {
        if (schoolGrade.getSYear() == null || schoolGrade.getSYear().equals("")){
            schoolGrade.setSYear(BaseController.getsYear() + "");
        }
        List<SchoolGrade> schoolGrades = this.schoolGradeDao.queryAllByLimit(schoolGrade);
        long count = this.schoolGradeDao.count(schoolGrade);
        if (count == 0) {
            return new ArrayList<>();
        }
        schoolGrades.get(0).setTotal(count);
        return schoolGrades;
    }
    /**
     *  获取学年列表
     */
    @Override
    public List<String> queryByYear(SchoolGrade schoolGrade) {
        return this.schoolGradeDao.queryYear(schoolGrade);
    }

    /**
     * 新增数据
     *
     * @param schoolGrade 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolGrade insert(SchoolGrade schoolGrade) {
        List<SchoolGrade> schoolGrades = this.schoolGradeDao.queryAllBy(schoolGrade.getSSchoolId(), schoolGrade.getSName(), schoolGrade.getSYear());
        if (schoolGrades.size() > 0){
            throw new RuntimeException("已存在: " + schoolGrade.getSName());
        }
        this.schoolGradeDao.insert(schoolGrade);
        return schoolGrade;
    }

    /**
     * 修改数据
     *
     * @param schoolGrade 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolGrade update(SchoolGrade schoolGrade) {
        this.schoolGradeDao.update(schoolGrade);
        return this.queryById(schoolGrade.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(Long id, String tableName) {
//        StudentArchives studentArchives1 = new StudentArchives();
//        studentArchives1.setTableName(tableName);
//        studentArchives1.setSGradeId(id);
//        // 查看是否有本学年本年级的学生档案记录
//        List<StudentArchives> studentArchives = archivesDao.queryAllByLimit(studentArchives1);
//        if (studentArchives.size() > 0) {
//            // 添加表名
//            studentArchives.get(0).setTableName(tableName);
//            // 批量删除学生
//            archivesDao.deleteByIds(studentArchives);
//        }
        // 查看是否有本学生的记录
        Student student = new Student();
        student.setSGradeId(id);
        student.setSize(Integer.MAX_VALUE);
        List<Student> students = studentDao.queryAllByLimit(student);
        if (students.size() > 0){
            studentDao.deleteByIds(students);
        }

        SchoolGradeClass schoolGradeClass = new SchoolGradeClass();
        schoolGradeClass.setSGradeId(id);
        // 查看是否有本年级的班级信息
        schoolGradeClass.setSize(Integer.MAX_VALUE);
        List<SchoolGradeClass> schoolGradeClasses = schoolGradeClassDao.queryAllByLimit(schoolGradeClass);
        if (schoolGradeClasses.size() > 0) {
            // 批量删除班级
            schoolGradeClassDao.deleteByIds(schoolGradeClasses);
        }
        // 删除年级
        return this.schoolGradeDao.deleteById(id) > 0;
    }
}
